home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / COLUMN.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  16.1 KB  |  541 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.lib.base_parent_interactor;
  4. import sub_arctic.lib.interactor;
  5. import sub_arctic.constraints.std_function;
  6. import sub_arctic.constraints.constraint;
  7. import sub_arctic.output.drawable;
  8. import sub_arctic.output.color_pair;
  9. import sub_arctic.lib.sub_arctic_error;
  10.  
  11. import java.awt.Graphics;
  12.  
  13. /**
  14.  * This object is a composition that puts its children in a tiled column 
  15.  * from top to bottom. It can be aligned with LEFT, RIGHT, or CENTER 
  16.  * justification.
  17.  *
  18.  * @author Ian Smith
  19.  */
  20. public class column extends base_parent_interactor {
  21.  
  22.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  23.    
  24.   /**
  25.    * This holds our state variable about whether or not we are
  26.    * sized by our children.  This variable can only be set at
  27.    * construction time.
  28.    */
  29.   protected boolean _size_by_children;
  30.  
  31.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  32.    
  33.   /**
  34.    * If this variable is set, the interactor has a box around it.
  35.    */
  36.   protected boolean _boxed=true;
  37.  
  38.   /**
  39.    * Return whether or not the interactor has a box around it.
  40.    * @return boolean true if the column has a box around it
  41.    */
  42.   public boolean boxed() { return _boxed;};
  43.  
  44.   /**
  45.    * Control whether or not the interactor has a box around it. 
  46.    *@param boolean b new state of boxedness
  47.    */
  48.   public void set_boxed(boolean b) { 
  49.     _boxed=b;
  50.     damage_self();
  51.   }
  52.  
  53.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  54.    
  55.   /**
  56.    * If this variable is set, the interactor has an opaque background.
  57.    */
  58.   protected boolean _opaque=true;
  59.  
  60.   /**
  61.    * Return whether or not the interactor has an opaque background
  62.    * @return boolean true if the column is opaque
  63.    */
  64.   public boolean opaque() { return _opaque;};
  65.  
  66.   /**
  67.    * Control whether or not the interactor has an opaque background
  68.    * @param boolean b new state of opaqueness
  69.    */
  70.   public void set_opaque(boolean b) { 
  71.     _opaque=b;
  72.     damage_self();
  73.   }
  74.  
  75.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  76.  
  77.   /** Border around all the children.  */
  78.   protected int _border;
  79.  
  80.   /** Return the border around all the children (in pixels).  
  81.   * @return int the size of the border in pixels */
  82.   public int border() {return _border;}
  83.  
  84.   /** Set border all around the children. 
  85.   * @param int v the size of the border around the children in pixels
  86.   */
  87.   public void set_border(int v) 
  88.     {
  89.       /* set the value then incorporate that back into constraints */
  90.       _border = v;
  91.       set_child_constraints();
  92.     }
  93.  
  94.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  95.  
  96.   /**
  97.    * This holds the color pair in use for this object. null indicates
  98.    * to use the system default color pair.
  99.    */
  100.   protected color_pair _colors;
  101.  
  102.   /**
  103.    * Return the current color pair. If you get null, we are using the
  104.    * system's default color pair. Note that the color pair for this
  105.    * interactor is <STRONG>not</STRONG> consulted if the object is
  106.    * not opaque.
  107.    *
  108.    * @return color_pair the current color pair
  109.    */
  110.   public color_pair colors() { 
  111.     if (_colors!=null) {
  112.       return _colors;
  113.     } else {
  114.       return manager.default_color_pair();
  115.     }
  116.   }
  117.  
  118.   /**
  119.    * Set the current color pair. If you set this to null, you'll get
  120.    * the system default color pair. 
  121.    * @param color_pair cp the new color_pair for this interactor
  122.    */
  123.   public void set_colors(color_pair cp) {
  124.     _colors=cp;
  125.     damage_self();
  126.   }
  127.  
  128.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  129.  
  130.   /** The spacing between children.  */
  131.   protected int _interchild_space;  
  132.  
  133.   /** 
  134.    * Return the spacing between children.  
  135.    * @return int the space between the children in the row (in pixels)
  136.    */
  137.   public int interchild_space() {return _interchild_space;}
  138.  
  139.   /** 
  140.    * Set the spacing between children. 
  141.    * @param int v the new interchild spacing
  142.    */
  143.   public void set_interchild_space(int v) 
  144.     {
  145.       /* set the value then incorporate that back into constraints */
  146.       _interchild_space = v;
  147.       set_child_constraints();
  148.     }
  149.  
  150.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  151.  
  152.   /** Constant to denote centered layout. */
  153.   public static final byte CENTER_JUSTIFIED = 0;
  154.  
  155.   /** Constant to denote left justified layout. */
  156.   public static final byte LEFT_JUSTIFIED = 1;
  157.  
  158.   /** Constant to denote right justified layout */
  159.   public static final byte RIGHT_JUSTIFIED = 2;
  160.  
  161.   /** The layout type for this column. */
  162.   protected byte _layout_type;
  163.  
  164.   /** 
  165.    * The layout type for this column.
  166.   * @return byte the type of layout in use in this interactor (must be one of 
  167.   *              the constants CENTER_JUSTIFIED, LEFT_JUSTIFIED, 
  168.   *              RIGHT_JUSTIFIED)
  169.   */
  170.   public byte layout_type() {return _layout_type;}
  171.  
  172.   /** 
  173.    * Set the layout type for this column.
  174.    * @param byte the type of layout you want (must be one of the constants 
  175.    *             CENTER_JUSTIFIED, TOP_JUSTIFIED, BOTTOM_JUSTIFIED)
  176.    */
  177.   public void set_layout_type(byte v) 
  178. {
  179.       /* verify layout type */
  180.       if ((v != CENTER_JUSTIFIED) && (v != LEFT_JUSTIFIED) && 
  181.       (v != RIGHT_JUSTIFIED)) 
  182.     {
  183.           throw new sub_arctic_error(
  184.         "Unrecognized layout type ("+v+") in set_layout_type()");
  185.         }
  186.  
  187.       /* set the value then incorporate that back into constraints */
  188.       _layout_type = v;
  189.       set_child_constraints();
  190.     }
  191.  
  192.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  193.  
  194.   /** 
  195.    * Full constructor for a column.
  196.    * 
  197.    * @param int        xv  the x position of this interactor.
  198.    * @param int        yv  the y position of this interactor.
  199.    * @param int        wv  the width of this interactor.
  200.    * @param int        hv  the height of this interactor.
  201.    * @param int        b   the border around the children of this column.
  202.    * @param int        ic  the inter-child spacing for this column.
  203.    * @param boolean    box whether or not you want a box around this interactor.
  204.    * @param boolean    o   whether or not the object has an opaque background.
  205.    * @param boolean    sbc pass true if this column's size should be controlled 
  206.    *                       by its children, false to use the size passed by 
  207.    *                       the user and/or constraints.
  208.    * @param byte       lt  the type of layout you want for this column (must 
  209.    *                       be one of the constants CENTER_JUSTIFIED, 
  210.    *                       RIGHT_JUSTIFIED, LEFT_JUSTIFIED).
  211.    * @param color_pair cp  the color pair to use if this object is opaque (if 
  212.    *                       you use null here you'll get the system's default 
  213.    *                       color pair).
  214.    */
  215.   public column(
  216.     int xv, int yv, 
  217.     int wv, int hv, 
  218.     int b,  int ic, 
  219.     boolean    box, 
  220.     boolean    o, 
  221.     boolean    sbc, 
  222.     byte       lt, 
  223.     color_pair cp) 
  224. {
  225.       super(xv,yv,wv,hv);
  226.  
  227.       /* stash values */
  228.       _boxed=box;
  229.       _opaque= o;
  230.       _border = b;
  231.       _interchild_space = ic;
  232.       _colors=cp;
  233.       _size_by_children=sbc;
  234.  
  235.       /* verify layout type */
  236.       if ((lt != CENTER_JUSTIFIED) && (lt != LEFT_JUSTIFIED) && 
  237.       (lt != RIGHT_JUSTIFIED)) 
  238.     {
  239.           throw new sub_arctic_error(
  240.         "Unrecognized layout type ("+lt+") in column()");
  241.         }
  242.       _layout_type = lt;
  243.     }
  244.  
  245.    //had:
  246.    //* @exception bad_value if the justification value is not recognized.
  247.    //* @exception general
  248.  
  249.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  250.    
  251.   /** 
  252.    * Smaller column constructor. This assumes that you want to just set the
  253.    * actual parameters of the column and you will use constraints to do all
  254.    * positioning.  It uses the system default colors and assumes 
  255.    * your width and height will be controlled by the children.
  256.    *
  257.    * @param int     b   the border around the children of this row
  258.    * @param int     ic  the inter-child spacing for this row
  259.    * @param boolean box whether or not you want a box around this interactor
  260.    * @param boolean o   whether or not the object is opaque
  261.    * @param byte    b   the type of layout you want for this row (must be one 
  262.    *                    of the constants CENTER_JUSTIFIED, TOP_JUSTIFIED, 
  263.    *                    BOTTOM_JUSTIFIED).
  264.    */
  265.   public column(int b, int ic, boolean box, boolean o, byte lt) 
  266. {
  267.       super(0,0,10,10);
  268.  
  269.       /* stash values */
  270.       _border = b;
  271.       _opaque= o;
  272.       _boxed=box;
  273.       _size_by_children=true;
  274.       _interchild_space = ic;
  275.       _colors=null;
  276.  
  277.       /* verify layout type */
  278.       if ((lt != CENTER_JUSTIFIED) && (lt != LEFT_JUSTIFIED) && 
  279.       (lt != RIGHT_JUSTIFIED)) 
  280.     {
  281.           throw new sub_arctic_error(
  282.         "Unrecognized layout type ("+lt+") in column()");
  283.         }
  284.       _layout_type = lt;
  285.     }
  286.  
  287.    //had:
  288.    //* @exception bad_value if the justification value is not recognized.
  289.    //* @exception general
  290.  
  291.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  292.  
  293.   /** 
  294.    * Minimal column constructor. This assumes that you want to use 
  295.    * constraints for position and children for size. It makes the column 
  296.    * not have a border and be opaque and have no whitespace border around 
  297.    * its edges.  This uses the system's default colors.
  298.    *
  299.    * @param int  ic the inter-child spacing for this column
  300.    * @param byte b  the type of layout you want for this column (must be one 
  301.    *                of the constants CENTER_JUSTIFIED, LEFT_JUSTIFIED, 
  302.    *                RIGHT_JUSTIFIED).
  303.    */
  304.   public column(int ic, byte lt) 
  305. {
  306.       super(0,0,10,10);
  307.  
  308.       /* stash values */
  309.       _border = 0;
  310.       _opaque= true;
  311.       _boxed= false;
  312.       _interchild_space = ic;
  313.       _size_by_children=true;
  314.       _colors=null;
  315.  
  316.       /* verify layout type */
  317.       if ((lt != CENTER_JUSTIFIED) && (lt != LEFT_JUSTIFIED) && 
  318.       (lt != RIGHT_JUSTIFIED)) 
  319.     {
  320.           throw new sub_arctic_error("Unrecognized layout type ("+lt+") in column()");
  321.         }
  322.       _layout_type = lt;
  323.     }
  324.  
  325.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  326.  
  327.   /** 
  328.    * Set the constraint for this object's width and height. 
  329.    */
  330.   public void set_local_constraints() 
  331. {
  332.       constraint width_constraint, height_constraint;
  333.  
  334.       /* if we are not sized by our children, then just don't do anything */
  335.        if (!_size_by_children) {
  336.     return;
  337.        }
  338.       /* now set up the width and height constraints of this object */
  339.       /* height is sum of height of children +  border */
  340.       /* note: this is NOT 2*border because we took one of them into account */
  341.       /* when laying out the first child's y position */
  342.       height_constraint = std_function.offset(LAST_CHILD.Y2(), border());
  343.  
  344.       /* width is max width + 2*border */
  345.       width_constraint = std_function.offset(MAX_CHILD.W(), 2*border());
  346.  
  347.       /* snap them on */
  348.       set_h_constraint(height_constraint);
  349.       set_w_constraint(width_constraint);
  350.     }
  351.  
  352.    //had:
  353.    //* @exception general PROPAGATED
  354.  
  355.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  356.  
  357.  
  358.   /** Establish or reestablish constraints on children to form a column.
  359.    */
  360.   protected void set_child_constraints() 
  361. {
  362.       int i, max = num_children();
  363.       interactor current_child;
  364.       constraint strut_between_children, xposition = null;
  365.  
  366.       /* loop over all children */
  367.       for (i=0; i<max; ++i) 
  368.     {
  369.           /* set the current child */
  370.           current_child = child(i);
  371.  
  372.           /* constraint syntax is: object code, part code, fun code, parm */
  373.  
  374.           /* set up the strut between children */
  375.           if (i != 0) 
  376.         {
  377.           /* this is the strut which keeps the y position of a child 
  378.            * below the previous child */
  379.           strut_between_children = std_function.offset(PREV_SIBLING.Y2(),
  380.                                        interchild_space());
  381.           current_child.set_y_constraint(strut_between_children);
  382.             } 
  383.       else 
  384.         {
  385.           /* i is zero, setup the top constraint */
  386.           strut_between_children = std_function.offset(PARENT.Y(), border());
  387.           current_child.set_y_constraint(strut_between_children);
  388.             }
  389.  
  390.           /* build x constraint based on layout type */
  391.           if (layout_type() == CENTER_JUSTIFIED) 
  392.         xposition=std_function.centered(PARENT.W(), 0);
  393.           else if (layout_type() == LEFT_JUSTIFIED) 
  394.         xposition = std_function.offset(PARENT.X(), border());
  395.           else if (layout_type() == RIGHT_JUSTIFIED) 
  396.         xposition = std_function.far_edge_just(PARENT.X2(), border());
  397.  
  398.       /* set the constraint */
  399.           current_child.set_x_constraint(xposition);
  400.         }
  401.  
  402.       /* set this object's own constraints */
  403.       set_local_constraints();
  404.   }
  405.  
  406.    //had:
  407.    //* @exception general PROPAGATED
  408.  
  409.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  410.  
  411.   /** 
  412.    * Draw the object on the provided drawable
  413.    * @param drawable d the surface to draw on 
  414.    */ 
  415.   protected void draw_self_local(drawable d) 
  416. {
  417.       
  418.       /* clear the background */
  419.       if (opaque()) {
  420.     d.setColor(colors().background());
  421.     d.fillRect(0,0,w()-1,h()-1);
  422.       }
  423.  
  424.       /* draw a bounding box */
  425.       if (boxed()) {
  426.     d.setColor(colors().foreground());
  427.     d.drawRect(0, 0, w()-1, h()-1); 
  428.       }
  429.  
  430.       draw_children(d);
  431.     }
  432.  
  433.    //had:
  434.    //* @exception general PROPAGATED 
  435.  
  436.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  437.  
  438.   /** 
  439.    * Overridden from base_interactor to also add constraints 
  440.    * @param int        at_indx the index position at which to change the child.
  441.    * @param interactor chld    the new child interactor for that position.
  442.    */
  443.   public void set_child(int at_indx, interactor chld) 
  444. {
  445.       super.set_child(at_indx,chld);
  446.       set_child_constraints();
  447.     }
  448.  
  449.    //had:
  450.    //* @exception op_not_supported PROPAGATED
  451.    //* @exception general PROPAGATED 
  452.  
  453.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  454.  
  455.   /** 
  456.    * Override from base_interactor to also add constraints 
  457.    * @param interactor chld the child interactor to add 
  458.    */
  459.   public void add_child(interactor chld) 
  460. {
  461.       super.add_child(chld);
  462.       set_child_constraints();
  463.     }
  464.  
  465.    //had:
  466.    //* @exception op_not_supported PROPAGATED
  467.    //* @exception general PROPAGATED 
  468.  
  469.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  470.  
  471.   /** 
  472.    * Override to also add constraints 
  473.    * @param int        at_indx the index position at which to insert the child
  474.    * @param interactor chld    the child interactor to insert
  475.    */
  476.  
  477.   public void insert_child(int at_indx, interactor chld) 
  478. {
  479.       super.insert_child(at_indx,chld);
  480.       set_child_constraints();
  481.     }
  482.  
  483.    //had:
  484.    //* @exception op_not_supported PROPAGATED
  485.    //* @exception general PROPAGATED 
  486.  
  487.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  488.  
  489.   /** 
  490.    * Override to also add constraints
  491.    * @param int at_indx the index position of the child to remove
  492.    */
  493.   public interactor remove_child(int at_indx) 
  494. {
  495.       interactor i;
  496.  
  497.       i=super.remove_child(at_indx);
  498.       set_child_constraints();
  499.       return i;
  500.     }
  501.  
  502.    //had:
  503.    //* @exception op_not_supported PROPAGATED
  504.    //* @exception general PROPAGATED 
  505.  
  506.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  507.  
  508.   /** 
  509.    * Override to also add constraints 
  510.    * @param interactor the_child the child to remove
  511.    */
  512.   public void remove_child(interactor the_child) 
  513. {
  514.       super.remove_child(the_child);
  515.       set_child_constraints();
  516.     }
  517.  
  518.    //had:
  519.    //* @exception op_not_supported PROPAGATED
  520.    //* @exception general PROPAGATED 
  521.  
  522.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  523. }
  524.  
  525. /*=========================== COPYRIGHT NOTICE ===========================
  526.  
  527. This file is part of the subArctic user interface toolkit.
  528.  
  529. Copyright (c) 1996 Scott Hudson and Ian Smith
  530. All rights reserved.
  531.  
  532. The subArctic system is freely available for most uses under the terms
  533. and conditions described in 
  534.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  535. and appearing in full in the lib/interactor.java source file.
  536.  
  537. The current release and additional information about this software can be 
  538. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  539.  
  540. ========================================================================*/
  541.